<template>
    <view>
        <custom :is_back="false">
            <template #backText>
                <view>购物车</view>
            </template>
            <template #right>
                <text class="margin-right-20" @tap="page.is_delete = !page.is_delete">{{page.is_delete?'完成':'管理'}}</text>
            </template>
        </custom>
        <navTabs :tab.sync="page.item_tab" :changeTab="key => page.item_tab.cur = key"></navTabs>
        <cart ref="cart" page_type="cart" :page_is_delete="page.is_delete" :paySubmit="paySubmit"></cart>
    </view>
</template>

<script>
    import cart from './components/cart.vue';
    export default {
        components: {
            cart,
        },
        data() {
            return {
                page: {
                    is_delete: false,
                    item_tab: {
                        cur: 'is_express',
                        list: {
                            is_express: {
                                title: '全国邮寄'
                            },
                            is_same_city: {
                                title: '同城配送',
                            },
                            is_to_shop: {
                                title: '到店自取',
                            }
                        }
                    },
                },
            }
        },
        watch: {
            'page.item_tab.cur'(new_value) {
                this.$refs.cart.pageInit(new_value);
            },
        },
        methods: {
            pageInit() {
                this.$refs.cart.pageInit(this.page.item_tab.cur);
            },
            paySubmit(buy_mode, pay_item_attr_ids, item_attr_list) {
                let pay_item_object = {};
                let pay_item_string = '';
                pay_item_attr_ids.forEach(item_attr_id => {
                    let seller_user_id = item_attr_list[item_attr_id].user_id;
                    if (!pay_item_object[seller_user_id]) {
                        pay_item_object[seller_user_id] = []
                    }
                    pay_item_object[seller_user_id].push(item_attr_id)
                })
                Object.keys(pay_item_object).forEach(seller_user_id => {
                    if (pay_item_string != '') {
                        pay_item_string += ';'
                    }
                    pay_item_string += seller_user_id + ':' + pay_item_object[seller_user_id].join(',')
                })
                this.$store.commit(
                    'userNavTo',
                    '/seller/item/pay?buy_mode=' + buy_mode + '&items=' + pay_item_string
                )
            }
        }
    }
</script>
